﻿using PTop.Code;
using PTop.Domain.Entity.Questionnaire;
using PTop.Domain.IRepository.Questionnaire;
using PTop.Repository.Questionnaire;
using System.Collections.Generic;

namespace PTop.Application.Questionnaire
{
    /// <summary>
    /// 调查问卷试题App
    /// </summary>	
    public class QnQuestionApp
    {
        private IQnQuestionRepository service = new QnQuestionRepository();

        public List<QnQuestionEntity> GetList(Pagination pagination, string pid, string keyword)
        {
            var expression = ExtLinq.True<QnQuestionEntity>();
            if (!string.IsNullOrEmpty(pid))
            {
                expression = expression.And(t => t.F_PaperId == pid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_PaperTitle.Contains(keyword));
                expression = expression.Or(t => t.F_Question.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }

        public List<QnQuestionEntity> GetListByPaper(string paperId)
        {
            var expression = ExtLinq.True<QnQuestionEntity>();
            expression = expression.And(t => t.F_PaperId == paperId);
            var p = new Pagination
            {
                page = 1,
                rows = 1000,
                sidx = "F_OrderId asc",
                sord = "asc"
            };
            return service.FindList(expression, p);
        }

        public QnQuestionEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void DeleteByPaperId(string paperId)
        {
            service.Delete(t => t.F_PaperId == paperId);
        }
        public void SubmitForm(QnQuestionEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                entity.Create();
                //entity.F_LastModifyTime = entity.F_CreatorTime;
                //entity.F_LastModifyUserId = entity.F_CreatorUserId;
                service.Insert(entity);
            }
        }

    }
}